package thread;


//单例模式--懒汉模式
// 懒 延迟创建实例 在计算机中的含义是褒义词 高效率
//懒汉模式线程不安全
class SingletonLazy{
    private static volatile SingletonLazy instance=null;//加上volatile 避免指令重排序 和内存可见性问题
    private static Object locker=new Object();

    public static SingletonLazy getInstance(){
        if(instance==null) {
            synchronized (locker) {
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    public synchronized static SingletonLazy getInstance1(){
        if(instance==null){
            instance=new SingletonLazy();
        }
        return instance;
    }

    SingletonLazy(){

    }
}
public class Demo26 {
    public static void main(String[] args) {
        SingletonLazy s1=SingletonLazy.getInstance();
        SingletonLazy s2=SingletonLazy.getInstance();
        System.out.println(s1==s2);
        //SingletonLazy s3=new SingletonLazy();
    }
}
